#!/usr/env python
# coding: utf-8

import hunspell
import unittest

class Kinyarwanda(unittest.TestCase):
  def setUp(self):
    self.hobj = hunspell.HunSpell('rw-RW_test.dic', 'rw-RW.aff')
    self.verbroot = 'rer'
    
  def testUnknownWord_come_ShouldFail(self):
    bad = self.hobj.spell('come')
    self.assertFalse(bad, 'come is not supposed to be in the test dictionary')
    
  def testRwRoot_rer_ShouldPass(self):
    good = self.hobj.spell('rer')
    self.assertTrue(good, 'rer root should be in the test dictionary')
  
  def testAffixRule_207_ForVerbs(self):
    expected_stem = [self.verbroot]
    stem = self.hobj.stem('ku%sa' % self.verbroot)
    self.assertEquals(expected_stem, stem, '"kurera", infinite form of "rer", should pass')
    
  def testCoverageUsingRealDictionaryAndCheckMinPrecision(self):
    self.hobj = hunspell.HunSpell('rw-RW.dic', 'rw-RW.aff')
    text = open('rw-RW_test.txt').read()
    tokens = text.split()  #Split using spaces between words
    min_precision = tokens[0]  #First token expected be the 
                               #minimum measured precision
    total = len(tokens)
    found_count = 0
    not_found = set()
    for token in tokens:
      if self.hobj.spell(token.strip(' ,.?;"%\'\r\n\t')):
        found_count += 1
      else:
        not_found.add(token)
    
    precision = 100*found_count/total
    print ('Correctly identified %d tokens out of %d\n'
           'Precision: %d%%' % (found_count, total, precision))
    print 'Could not find: %s' % '; '.join(sorted(list(not_found)))
    self.assertTrue(int(min_precision) <= precision, 
                    'At least %s%% precision expected' % min_precision)


if __name__ == '__main__':
  unittest.main()
